class Solution {
public:
    int maximumBeauty(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int begin = 0, end = 0, ret = 0;
        while (end < nums.size())
        {
            if (nums[end] - nums[begin] > 2 * k)
            {
                ret = max(ret, end - begin);
                begin = end;
            }
            end++;
        }
        return ret;
    }
};